package com.cdtye.itps.cms.service.warn;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdtye.common.mybatiscore.base.BaseService;
import com.cdtye.common.webcore.constants.WebConstants;
import com.cdtye.common.webcore.web.BizException;
import com.cdtye.itps.cms.dao.enums.DelFlagEnum;
import com.cdtye.itps.cms.dao.warn.entity.JcjcWarnInformation;
import com.cdtye.itps.cms.dao.warn.vo.JcjcWarnInformationVo;
import com.cdtye.itps.cms.mapper.warn.JcjcWarnInformationMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class JcjcWarnInformationService extends BaseService<JcjcWarnInformationMapper, JcjcWarnInformation> {
    @Autowired
    private JcjcWarnInformationMapper modelMapper;

    @Autowired
    private JcjcWarnFilesService jcjcWarnFilesService;

    /**
     * 更新
     */
    public Object saveOrUpdate(JcjcWarnInformationVo vo){
        if (StringUtils.isBlank(vo.getWarnType())){
            throw new BizException("文章类型不能为空");
        }
        String id  = vo.getId();
        JcjcWarnInformation jcjcWarnInformation = this.getWarnById(id);
        if(null==jcjcWarnInformation){
            jcjcWarnInformation = new JcjcWarnInformation();
        }
        BeanUtils.copyProperties(vo, jcjcWarnInformation);
        jcjcWarnInformation.setContent(StringUtils.isEmpty(vo.getContentString())?new byte[0]:vo.getContentString().getBytes(StandardCharsets.UTF_8));
        if (StringUtils.isBlank(jcjcWarnInformation.getId())) {
            //新增
            this.insertJcjcWarnInformation(jcjcWarnInformation);
            if (!CollectionUtils.isEmpty(vo.getImagesList())) {//插入附件
                jcjcWarnFilesService.insertJcjcWarnFiles(vo.getImagesList(), jcjcWarnInformation.getId(),vo.getCreateBy());
            }
        } else {
            modelMapper.updateById(jcjcWarnInformation);
            if (!CollectionUtils.isEmpty(vo.getImagesList())) {//插入附件
                jcjcWarnFilesService.insertJcjcWarnFiles(vo.getImagesList(), jcjcWarnInformation.getId(), vo.getCreateBy());
            } else {
                jcjcWarnFilesService.deleteByBizId(jcjcWarnInformation.getId());
            }
        }
        return this.getById(jcjcWarnInformation.getId());
    }

    /**
     * 插入
     */
    public void insertJcjcWarnInformation(JcjcWarnInformation model)
    {
        model.setDelFlag("N");
        model.setUpdateBy(model.getCreateBy());
        Date dNow = new Date( );
        model.setCreateDatetime(dNow);
        model.setUpdateDatetime(dNow);
        modelMapper.insert(model);
    }

    /**
     * 逻辑删除
     */
    public void deleteData(String id) throws Exception {
        //删除文件
        jcjcWarnFilesService.deleteByBizId(id);
        //删除预警信息
        JcjcWarnInformation jcjcWarnInformation = getById(id);
        jcjcWarnInformation.setDelFlag("Y");
        this.updateById(jcjcWarnInformation);
    }

    /**
     * 列表查询
     */
    public List<JcjcWarnInformation> list(JcjcWarnInformationVo vo){
        LambdaQueryWrapper<JcjcWarnInformation> wrapper = new LambdaQueryWrapper<>();
        //添加查询条件

        //物理删除表,查询非删除状态包括null
        wrapper.and(i->i.eq(JcjcWarnInformation::getDelFlag, WebConstants.NO).or().isNull(JcjcWarnInformation::getDelFlag));
        return list(wrapper);
    }

    /**
     * 分页查询，自定义sql语句
     */
    public IPage<JcjcWarnInformation> listPage(JcjcWarnInformationVo vo){
        IPage<JcjcWarnInformation> all = modelMapper.getAll(vo.page(),vo);
        try {
            if (!CollectionUtils.isEmpty(all.getRecords())) {
                all.getRecords().forEach(jcjcWarnInformationPo -> {
                    jcjcWarnInformationPo.setContentString(jcjcWarnInformationPo.getContentByte() == null ? null : new String(jcjcWarnInformationPo.getContentByte()));
                });
            }
            return all;
        } catch (Exception e) {
            throw new BizException("内容转换失败");
        }
    }

    /**
     * 获取预警信息类型
     */
    public List<Map<String,Object>> getWarnType()
    {
        return modelMapper.getWarnType();
    }

    /**
     * 根据id获取预警信息
     */
    public JcjcWarnInformation getWarnById(String id){
        if (StringUtils.isBlank(id)){
            return null;
        }
        QueryWrapper<JcjcWarnInformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("DEL_FLAG", DelFlagEnum.NO.value);
        JcjcWarnInformation jcjcWarnInformation = this.getOne(queryWrapper);
        if (jcjcWarnInformation == null) {
            throw new BizException("该记录不存在");
        }
        return jcjcWarnInformation;
    }

    /**
     * 获取预警信息摘要
     */
    public List<JcjcWarnInformationVo> getEarlyWarning(){
        List<JcjcWarnInformationVo> list=new ArrayList<>();
        JcjcWarnInformationVo vo=new JcjcWarnInformationVo();
        List<JcjcWarnInformation> result=this.listPage(vo).getRecords();
        result.stream().forEach(a->{
            JcjcWarnInformationVo v=new JcjcWarnInformationVo();
            BeanUtils.copyProperties(a,v);
            list.add(v);
        });
        return list;
    }
}
